package com.yvon.maple.service.base.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * aliyun 存储
 *
 * @author : Yvon
 * @version : 1.0
 * @since : 2020-11-03
 */
public class AliyunStorage implements Storage {
    private final OSSClient ossClient;

    public AliyunStorage(OSSClient ossClient) {
        this.ossClient = ossClient;
    }

    @Override
    public boolean putObject(String bucketName, String objectName, InputStream inputStream, String contentType) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(contentType);
        this.ossClient.putObject(bucketName, objectName, inputStream, objectMetadata);
        return true;
    }



    @Override
    public InputStream getObject(String bucketName, String objectName) {
        return this.ossClient.getObject(bucketName, objectName).getObjectContent();
    }

    @Override
    public String getTempUrl(String bucketName, String objectName, int duration, TimeUnit unit) {
        URL url = ossClient.generatePresignedUrl(bucketName, objectName, new Date(System.currentTimeMillis() + unit.toMillis(duration)));
        return url.toString();
    }

    @Override
    public void removeObject(String bucketName, String objectName) {
        this.ossClient.deleteObject(bucketName, objectName);
    }

    @Override
    public void removeObjects(String bucketName, List<String> objectNames) throws Exception {

    }
}